package de.godlike.fxbox.presentation;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.layout.GridPane;
import javafx.stage.Popup;
import de.godlike.fxbox.persistence.dao.ServerConfigDoa;

public class NewHostSystemWindow extends Popup {
	private final Popup me = this;

	public NewHostSystemWindow() {
		init();
	}

	public void init() {
		GridPane layout = new GridPane();
		
		/*
		 * FxboxMainWindow
		 */
		final FxboxMainWindow mainWindow = FxboxMainWindow.getInstance();
		
		/*
		 * Name-Label
		 */
		Label displaynameLabel = new Label("Name:");

		/*
		 * Textfield for name input
		 */
		final TextField displaynameInput = new TextField("Test-Server");

		/*
		 * Url-Label
		 */
		Label urlLabel = new Label("Url:");

		/*
		 * Textfield for url input
		 */
		final TextField urlInput = new TextField("http://localhost:18083");
		
		/*
		 * Url-Label
		 */
		Label userLabel = new Label("Benutzer:");

		/*
		 * Textfield for url input
		 */
		final TextField userInput = new TextField("default");
		
		/*
		 * Url-Label
		 */
		Label passwordLabel = new Label("Password:");

		/*
		 * Textfield for url input
		 */
		final TextField passwordInput = new TextField("password");

		/*
		 * Button to test the connection
		 */
		Button btnTestConnection = new Button("Verbindung Testen");
		btnTestConnection.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent arg0) {

			}
		});

		/*
		 * Button to test the connection
		 */
		Button btnAbortConnection = new Button("Abbrechen");
		btnAbortConnection.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent arg0) {
				mainWindow.getRoot().getScene().getRoot()
						.setDisable(false);
				mainWindow.getRoot().getScene().getRoot().setEffect(null);
				me.hide();
			}
		});

		/*
		 * Button to save the connection
		 */
		Button btnSaveConnection = new Button("Verbindung Speichern");
		btnSaveConnection.setOnAction(new EventHandler<ActionEvent>() {

			public void handle(ActionEvent arg0) {
				ServerConfigDoa serverConfig = ServerConfigDoa.getInstance();
				serverConfig.addNewServer(displaynameInput.getText(),
						urlInput.getText(), userInput.getText(), passwordInput.getText());

				mainWindow.getRoot().getScene().getRoot()
						.setDisable(false);
				mainWindow.getRoot().getScene().getRoot().setEffect(null);
				me.hide();
			}
		});

		/*
		 * Layout specific
		 */
		layout.add(displaynameLabel, 0, 0, 2, 1);
		layout.add(displaynameInput, 0, 1, 2, 1);
		layout.add(urlLabel, 0, 2, 2, 1);
		layout.add(urlInput, 0, 3, 2, 1);
		layout.add(userLabel, 0, 4, 2, 1);
		layout.add(userInput, 0, 5, 2, 1);
		layout.add(passwordLabel, 0, 6, 2, 1);
		layout.add(passwordInput, 0, 7, 2, 1);
		layout.add(btnTestConnection, 0, 8, 1, 1);
		layout.add(btnAbortConnection, 1, 9, 1, 1);
		layout.add(btnSaveConnection, 0, 9, 2, 1);

		centerOnScreen();
		getContent().add(layout);
		mainWindow.getRoot().getScene().getRoot()
				.setEffect(new GaussianBlur());
		mainWindow.getRoot().getScene().getRoot().setDisable(true);
		show(mainWindow.getRoot().getScene().getWindow());
		this.centerOnScreen();
	}
}
